package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleManageService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("/role")
@RestController
public class RoleManageController {
    @Reference
    RoleManageService roleManageService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return roleManageService.findPage(pageBean);
    }

    //查询出所有的权限
    @RequestMapping("/findPermission")
    public Result findPermission() {
        return roleManageService.findPermission();
    }


    //添加角色以及相关权限
    @RequestMapping("/addRole")
    public Result addRole(Integer[] permissionIds, @RequestBody Role role) {

        try {
            roleManageService.addRole(permissionIds, role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //添加角色以及相关权限
    @RequestMapping("/getRole")
    public Result getRole(int id) {

        Role role = roleManageService.getRole(id);
        if (role != null) {
            return new Result(true, MessageConstant.GET_ROLE_LIST_SUCCESS, role);
        }
        return new Result(true, MessageConstant.GET_ROLE_LIST_FAIL);
    }

    //根据查询到的角色id查询对应的权限id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> permissionIds = roleManageService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.GET_PREMISSIONIDS_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_PREMISSIONIDS_FAIL);
        }

    }

    //根据获取到的权限数组以及角色信息进行修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleManageService.edit(role, permissionIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_ROLE_FAIL);
        }
    }


    @RequestMapping("/delete")
    public Result delete(int id) {
        try {
            roleManageService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
